# -*- coding: utf-8 -*-
"""
@Time ： 2021/1/19 14:14
@Auth ： 张张呀
@File ：laoshi.py
@IDE ：PyCharm
@Motto：ABC(Always Be Coding)
"""
import requests
import xlwt


class dou_ban:
    def __init__(self):
        self.url = 'https://movie.douban.com/j/chart/top_list?type=11&interval_id=100%3A90&action='

        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'}
        self.movie_id = []
        self.movie_name = []
        self.movie_pic_url = []
        self.movie_types = []
        self.movie_regions = []
        self.movie_url = []
        self.movie_score = []
        self.movie_release_date = []

    # 储存到excel表格中
    def saveDate(self, sheet, all_list, book, savepath, page):

        col = ("ID", "标题", "封面图片地址", "电影类型", "电影国别", "电影地址", "电影评分", "上映时间")

        for i2 in range(0, 8):
            sheet.write(0, i2, col[i2])  # 列名
        for i3 in range(0, page):
            b = (all_list[0])[i3]
            c = (all_list[1])[i3]
            d = (all_list[2])[i3]
            e = (all_list[3])[i3]
            f = (all_list[4])[i3]
            g = (all_list[5])[i3]
            h = (all_list[6])[i3]
            j = (all_list[7])[i3]
            print("第%d条输入完成" % (i3 + 1))
            sheet.write((i3 + 1), 0, b)
            sheet.write((i3 + 1), 1, c)
            sheet.write((i3 + 1), 2, d)
            sheet.write((i3 + 1), 3, e)
            sheet.write((i3 + 1), 4, f)
            sheet.write((i3 + 1), 5, g)
            sheet.write((i3 + 1), 6, h)
            sheet.write((i3 + 1), 7, j)

        book.save(savepath)

    def get_data(self):
        n = 0
        for pp in range(10):
            url = f'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start={pp}&limit=300'
            response = requests.get(url, headers=self.headers).json()
            for movie in response:
                self.movie_id.append(movie['id'])
                self.movie_name.append(movie['title'])
                self.movie_pic_url.append(movie['cover_url'])
                self.movie_types.append(movie['types'])
                self.movie_regions.append(movie['regions'])
                self.movie_url.append(movie['url'])
                self.movie_score.append(movie['score'])
                self.movie_release_date.append(movie['release_date'])
                n += 1
        all_list = [self.movie_id, self.movie_name, self.movie_pic_url, self.movie_types, self.movie_regions,
                    self.movie_url, self.movie_score, self.movie_release_date]
        return n, all_list

    def run(self):
        savepath = "豆瓣电影.xls"  # 定义保存路径
        book = xlwt.Workbook(encoding="utf-8", style_compression=0)  # 创建workbook对象
        sheet = book.add_sheet('豆瓣电影', cell_overwrite_ok=True)  # 创建工作表
        n, all_list = self.get_data()
        self.saveDate(sheet, all_list, book, savepath, n)


if __name__ == '__main__':
    dou_ban().run()
